package com.mall.cart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.cart.entity.dto.CartItemDTO;
import com.mall.cart.entity.po.Cart;
import com.mall.cart.entity.vo.CartItemVO;
import com.mall.cart.entity.vo.CartSummaryVO;
import com.mall.cart.mapper.CartMapper;
import com.mall.cart.service.CartService;
import com.mall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    private final CartMapper cartMapper;
    /*
     * 添加购物车
     * */
    @Override
    public void addItemToCart(CartItemDTO cartItemDTO) {
        // 1.获取登录用户id
        Long userId = UserContext.getUser();
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userId).eq(Cart::getProductId, cartItemDTO.getProductId());
        Cart cart = getOne(queryWrapper);

        if (cart == null) {
            cart = new Cart();
            cart.setUserId(userId);
            cart.setProductId(cartItemDTO.getProductId());
            cart.setQuantity(cartItemDTO.getQuantity());
            cart.setChecked(true); // 默认选中
            save(cart);
        } else {
            cart.setQuantity(cartItemDTO.getQuantity());
            updateById(cart);
        }
    }
    /*
     * 查看购物车
     * */
    @Override
    public CartSummaryVO getCartSummary(Long userId) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userId).eq(Cart::getChecked, true);
        List<Cart> carts = this.list(queryWrapper);

        List<CartItemVO> itemVOS = new ArrayList<>();
        BigDecimal totalAmount = BigDecimal.ZERO;

        for (Cart cart : carts) {
            CartItemVO itemVO = new CartItemVO();
            itemVO.setProductId(cart.getProductId());
            itemVO.setQuantity(cart.getQuantity());
            itemVO.setSelected(cart.getChecked());
            // 假设有一个方法获取商品价格
            BigDecimal price = getProductPrice(cart.getProductId());
            itemVO.setPrice(price);
            itemVO.setTotalPrice(price.multiply(BigDecimal.valueOf(cart.getQuantity())));
            itemVOS.add(itemVO);
            totalAmount = totalAmount.add(itemVO.getTotalPrice());
        }

        CartSummaryVO summaryVO = new CartSummaryVO();
        summaryVO.setItems(itemVOS);
        summaryVO.setTotalAmount(totalAmount);

        return summaryVO;
    }
    /*
     * 修改购物车商品数量
     * */
    @Override
    public void updateCartItemQuantity(Long userId, Long productId, Integer quantity) {
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userId).eq(Cart::getProductId, productId);
        Cart cart = this.getOne(queryWrapper);
        if (cart != null) {
            cart.setQuantity(quantity);
            this.updateById(cart);
        }
    }

    private BigDecimal getProductPrice(Long productId) {
        // 模拟获取商品价格的方法，实际应用中应从商品服务或数据库中获取
        return BigDecimal.valueOf(99.99);
    }
}